package com.zhdl.modules.process;

import com.zhdl.common.ytools.callback.BoolCallback;
import com.zhdl.common.ytools.callback.CommonCallback;
//import com.zhdl.modules.process.task.DynamicTask;
import com.zhdl.modules.process.dto.MyEx;
import com.zhdl.modules.process.dto.ScheduleData;
import com.zhdl.modules.process.dto.VerifyData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public abstract class BaseProcess implements CommonProcess {
    public boolean isStart = false, isRunning = false, initComplete = false, isStop = false;
    public boolean allAlarm = false;
    public int process;
    public List<MyEx> alarmList = new ArrayList<>();
    public int yield = -1;//本次开机后产量
    //线程池
    @Resource
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    //任务管理器
    @Resource
//    DynamicTask dynamicTask;
    List<BaseProcess> childList = new ArrayList<>();
    private boolean isAuto = false;// 手自动切换
    private final ScheduleData scheduleData = new ScheduleData();

    public void setAuto(boolean auto) {
        isAuto = auto;
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.setAuto(auto);
            }
        }
        if (!auto) {
            stop();
        }
    }

    @Override
    public void initLocation(CommonCallback<String> complete) {
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.initLocation(complete);
            }
        }
    }

    public VerifyData getVerifyByStep(Integer step, List<VerifyData> verifyDataList) {
        VerifyData verify = null;
        for (VerifyData verifyData : verifyDataList) {
            if (verifyData.getStep() == step.intValue()) {
                verify = verifyData;
                break;
            }
        }
        return verify;
    }


    public boolean checkStart(Integer process) {
        if (process == 0 && isStop) {
            isStart = false;
            isRunning = false;
            isStop = false;
            this.process = process;
            log.debug("流程结束-->" + "流程启动停止逻辑完成上一次周期暂停");
            return false;
        }
        if (!isStart && isRunning) {
            //暂停后未完成的任务完成后 不再继续
            this.process = process;
            isRunning = false;
            return false;
        }
        if (!isStart || !isRunning || !initComplete || !isAuto) {
            log.info("不满足流程继续条件isStart: " + isStart + "-----isRunning: " + isRunning + "------initComplete: " + initComplete + "-------isAuto: " + isAuto);
            return false;
        }
        if (process == 0) {
            yield++;
            log.info("当前运行产量: " + yield);
        }
        this.process = process;
        return true;
    }

    @Override
    public boolean checkStartItems() {
        return true;
    }

    //开始流程
    @Override
    public void start() {
        if (allAlarm) {
            log.info("先复位报警!!");
            return;
        }
        if (!isAuto) {
            log.info("请先切换到自动模式");
            return;
        }
        if (!initComplete) {
            log.info("初始化未完成!!");
            return;
        }
        if (isRunning) {
            log.info("流程正在进行 无需重复start");
            return;
        }
        //流程开始标志
        isStart = true;
        //流程运行标志
        isRunning = true;
        //流程开始时，判断是否是由暂停开始
        if (process == -1) {
            process = 0;
        }
        startProcess(0);
    }

    //暂停流程
    @Override
    public void stop() {
        //停止标志
        isStop = true;
        //判断子流程是否正在运行
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.stop();
            }
        }
    }

    //继续流程
    @Override
    public void onPause() {
        //状态标为未启动
        isStart = false;
        //状态标为停止中
        isStop = false;
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.onPause();
            }
        }
    }

    //结束重置状态
    public void release() {
        isStart = false;
        isRunning = false;
        initComplete = false;
        isStop = false;
        process = -1;
//        dynamicTask.stopAll();
        yield = -1;
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.release();
            }
        }
    }

    //打手动
    private boolean canManual() {
        return !isRunning && !isAuto;
    }

    //打自动
    private boolean canAuto() {
        return !isRunning && isAuto;
    }

    //设置报警信息
    @Override
    public void setExCallBack(CommonCallback<MyEx> callback) {
    }

    //添加报警信息
    public void addAlarm(MyEx myEx) {
        allAlarm = true;
        onPause();
        alarmList.add(myEx);
    }

    //报警复位
    @Override
    public void cancelAlarm() {
        allAlarm = false;
        alarmList.clear();
        if (!childList.isEmpty()) {
            for (BaseProcess baseProcess : childList) {
                baseProcess.cancelAlarm();
            }
        }
    }

    //传递SSE数据
    @Override
    public void setSSEData(CommonCallback<String> callback) {

    }

    /**
     * 开启校验
     */
    @Override
    public void openCheck(CommonCallback<Map<String, String>> callback) {

    }

    /**
     * 流程进度回调
     *
     * @param callback 流程步数回调
     */
    @Override
    public void processCallBack(BoolCallback<Integer> callback) {

    }

    /**
     * 队列
     *
     * @param completeMap 队列
     */
    @Override
    public void setCompleteMap(Map<Integer, Boolean> completeMap) {

    }

}
